﻿using FTBS.Models.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace FTBS.Models.DAO
{
    public class CategoryDAO
    {
        private FilmTicketBookingSystemEntities _entities = new FilmTicketBookingSystemEntities();

        /// <summary>
        /// Load all category
        /// </summary>
        /// <returns>List of Category</returns>
        /// <author>VuongVT</author>
        public List<Category> LoadCategories()
        {
            List<Category> listCategory = new List<Category>();
            try
            {
                var categories = from category in _entities.Categories
                                 where category.IsDelete == false
                                 select category;
                if (categories != null)
                {
                    listCategory = categories.ToList();
                }
            }
            catch
            {
                return listCategory;
            }
            return listCategory;
        }

        /// <summary>
        /// Load Category By Id
        /// </summary>
        /// <returns>Category</returns>
        /// <author>VuongVT</author>
        public Category LoadCategoryById(int id)
        {
            Category category = new Category();
            try
            {
                category = (from categoryEnt in _entities.Categories
                        where categoryEnt.CategoryId == id
                             && categoryEnt.IsDelete == false
                        select categoryEnt).FirstOrDefault();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return category;
        }

        /// <summary>
        /// Add a Category
        /// </summary>
        /// <returns>1: Success -- 0: Fail</returns>
        /// <author>VuongVT</author>
        public int Add(Category category)
        {
            try
            {
                _entities.Categories.Add(category);
                int resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                return 0;
                throw ex;
            }
        }

        /// <summary>
        /// Edit a Category
        /// </summary>
        /// <returns>1: Success -- 0: Fail</returns>
        /// <author>VuongVT</author>
        public int Edit(Category category)
        {
            try
            {
                _entities.Entry(category).State = EntityState.Modified;
                int resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                return 0;
                throw ex;
            }
        }

        /// <summary>
        /// Delete a Category
        /// </summary>
        /// <returns>1: Success -- 0: Fail</returns>
        /// <author>VuongVT</author>
        public int Delete(Category category)
        {
            try
            {
                category.IsDelete = true;
                _entities.Entry(category).State = EntityState.Modified;
                int result = _entities.SaveChanges();
                return result;
            }
            catch (Exception ex)
            {
                return 0;
                throw ex;
            }
        }

        /// <summary>
        /// check exist category
        /// </summary>
        /// <param name="category"></param>
        /// <returns>true: existed; false: not existed</returns>
        /// <author>hungph</author>
        public bool CheckIsExisted(Category category)
        {
            try
            {
                var isExist = _entities.Categories.Any(c => c.IsDelete == false && c.CategoryName == category.CategoryName);
                return isExist;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}